package com.hkyl.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.hkyl.hospital.domain.ResultVo;
import com.hkyl.hospital.entity.User;
import com.hkyl.hospital.service.FileService;
import com.hkyl.hospital.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author gao
 * @date 2023/07/09 11:23:53
 */
@RestController

public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private FileService fileService;

    @PostMapping("user/save")
    public ResultVo save(@ModelAttribute User user, MultipartFile photoData, HttpServletRequest request) throws IOException {
        String uploadpath = request.getRealPath("/images");
        Map<String, String> map = fileService.upload(photoData, uploadpath);
        user.setPhoto(map.get("filename"));
        userService.save(user);
        return ResultVo.success("save.ok");

    }
    @PostMapping("user/findAll")
    public ResultVo findAll() {
        List<User> list = userService.findAll();
        return  ResultVo.success(list);
    }
    @PostMapping("user/findSomeByPage/{pageNum}/{pageSize}")
    public ResultVo findSomeByPage(@PathVariable Integer pageNum,@PathVariable Integer pageSize) {
        List<User> list = userService.findSomeByPage(pageNum,pageSize);
        PageInfo<User>pageInfo=new PageInfo<>(list);
        return  ResultVo.success(pageInfo);
    }
    @PostMapping("user/delete/{id}/{filename}")
    public ResultVo delete(@PathVariable("id") String idStr, @PathVariable("filename") String filename, HttpServletRequest request) {
        int id;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            // 处理无法将 id 转换为整数的情况
            return ResultVo.failure("Invalid id value");
        }
        int i = userService.delete(id);
        String uploadpath = request.getRealPath("/images");
        String filepath=uploadpath+"/"+filename;
        fileService.delete(filepath);
        return i > 0 ? ResultVo.success("delete.ok") : ResultVo.failure("delete not ok");
    }

    @PostMapping("user/update")
    public ResultVo update(@ModelAttribute User user,MultipartFile photoData,HttpServletRequest request) throws IOException {
        if(photoData!=null){
            String oldPhotoName = userService.findOne(user.getUserId()).getPhoto();
            String uploadpath = request.getRealPath("/images");
            if(oldPhotoName!=null) {
                File oldFile = new File(uploadpath, oldPhotoName);
                if (oldFile.exists()) {
                    oldFile.delete();
                }
            }
                Map<String, String> map = fileService.upload(photoData, uploadpath);
                user.setPhoto(map.get("filename"));

        }
        userService.update(user);
        return ResultVo.success("update.ok");
    }
    @PostMapping("user/upload")
    public ResultVo upload(MultipartFile photo, HttpServletRequest request) throws IOException {
        String uploadpath = request.getRealPath("/images");
        Map<String, String> map = fileService.upload(photo, uploadpath);
        return ResultVo.success(map);
    }

}
